package cn.tedu._07springaop.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

import static com.fasterxml.jackson.databind.cfg.CoercionInputShape.Array;

@Aspect
@Component
public class LogAspect {

    //拦截多个侵权
    /*@Pointcut("execution(public int cn.tedu._07springaop.aop.CalculatorImpl.add(..))" +
            "|| execution(public int cn.tedu._07springaop.aop.CalculatorImpl.sub(..))")*/
    //粗粒度：bean()
    //@Pointcut("bean(calculatorImpl)")

    //粗粒度：within()
    @Pointcut("within(cn.tedu._07springaop.aop.CalculatorImpl)")
    public void pointcut(){}

   /* @Before("pointcut()")
    public void beforeMethod(JoinPoint joinpoint){
        String methodName = joinpoint.getSignature().getName();
        String args = Arrays.toString(joinpoint.getArgs());
        System.out.println(methodName+"方法开始了，参数为:"+ args);
    }

    @After("pointcut()")
    public void afterMethod(JoinPoint joinpoint){
        String methodName = joinpoint.getSignature().getName();
        System.out.println("后置通知"+methodName+"方法结束了");
    }

    @AfterReturning (value = "pointcut()",returning = "result")
    public void afterReturning(JoinPoint joinpoint,Object result){
        String methodName = joinpoint.getSignature().getName();
        System.out.println("返回通知"+methodName+"方法正常返回了,结果是:"+result);
    }

    @AfterThrowing (value = "pointcut()",throwing = "ex")
    public void afterThrowing(JoinPoint joinpoint,Throwable ex){
        String methodName = joinpoint.getSignature().getName();
        System.out.println("异常通知"+methodName+"方法异常了,异常是:"+ex.getMessage());
    }*/


    @Around("pointcut()")
    public Object aroundMethod(ProceedingJoinPoint joinpoint){
        Object result = null;
        try {
            System.out.println("环绕通知开始");
            result = joinpoint.proceed();
            System.out.println("环绕通知返回");
        }catch (Throwable e){
            System.out.println("环绕通知异常");
        }finally {
            System.out.println("环绕通知结束");
        }return result;
    }



}
